<?php
namespace app\work\controller;

use \think\Controller;
use \think\Db;
class My extends Base
{
    /**
     * 我的信息 myInfo
     * name,avatar
     */
    public function myInfo()
    {
		extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $data = Db::table('up_work_user')
            ->where('user_id', $user_id)
            ->find();
        return json_encode(['status'=>200,'msg'=>$data]);
    }
    /**
     * 修改头像或者昵称：updateMyInfo
     * name,avatar
     */
    public function updateMyInfo()
    {
		extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $update = array();
        if(isset($name)) {
            $update['user_name'] = $name;
        }
        if(isset($avatar)){
            $update['user_image'] = $avatar;
        }
        Db::table('up_work_user')
            ->where('user_id', $user_id)
            ->update($update);
        return json_encode(['status'=>200,'msg'=>'修改成功']);
    }
    /**
     *
     * 添加银行卡:addAccount
     */
    public function addAccount(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $right = httpGetCheckAccName($number,$name);
        if($right['result']['res'] != 1){
            return json_encode(['status'=>0,'msg'=>'卡号和姓名不匹配']);
        }
        $data = array();
        $data['acc_user_id'] = $user_id;
        $data['acc_username'] = $name;
        $data['acc_idcard'] = $idcard;
        $data['acc_bank'] = $bank;
        $data['acc_number'] = $number;
        $res = Db::table('up_work_account')->insert($data);
        if($res){
            return json_encode(['status'=>200,'msg'=>'提交成功']);
        }else{
            return json_encode(['status'=>0,'msg'=>'提交失败']);
        }
    }
    /**
     *
     * 我的银行卡:myAccount
     */
    public function myAccount(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $data = array();
        $data['acc_user_id'] = $user_id;
        $res = Db::table('up_work_account')->where($data)->select();
        return json_encode(['status'=>200,'msg'=>$res]);
    }
    /**
     *
     * 编辑银行卡:editAccount
     */
    public function editAccount(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $right = httpGetCheckAccName($number,$name);
        if($right['result']['res'] != 1){
            return json_encode(['status'=>0,'msg'=>'卡号和姓名不匹配']);
        }
        $data = array();
        $data['acc_username'] = $name;
        $data['acc_idcard'] = $idcard;
        $data['acc_bank'] = $bank;
        $data['acc_number'] = $number;
        $res = Db::table('up_work_account')->where('acc_id',$acc_id)->update($data);
        if($res){
            return json_encode(['status'=>200,'msg'=>'成功']);
        }else{
            return json_encode(['status'=>0,'msg'=>'失败']);
        }
    }
    /**
     *
     * 删除银行卡:delAccount
     */
    public function delAccount(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $res = Db::table('up_work_account')->where('acc_id',$acc_id)->delete();
        if($res){
            return json_encode(['status'=>200,'msg'=>'成功']);
        }else{
            return json_encode(['status'=>0,'msg'=>'失败']);
        }
    }
    /**
     * 我的业绩:myShops
     * page  type(若存在并且为1，显示当天的)  self（不存在查所有，存在的话：1自己  2二级 3三级）
     */
    public function myShops(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $today = date('Y-m-d');
        $where = array();
        $where['o_user_id'] = $user_id;
        $where['o_status'] = 2;
        if(isset($type) && $type == 1){
            $where['o_date'] = $today;
        }
        if(isset($self)){//不存在查所有，存在的话：1自己  2二级 3三级
            $where['o_level'] = $self;
        }
        $field = 'o_id,o_level,o_money,s_pinlei,s_name,s_address,s_join_in,s_join_end';
        $data = Db::table('up_work_order')->alias('o')
            ->join('up_work_shops_copy sc','o.o_number=sc.s_ordernumber','LEFT')
            ->where($where)
            ->field($field)
            ->order('o_datetime DESC')
            ->page($page,10)
            ->select();
        return json_encode(['status'=>200,'msg'=>$data]);
    }

    /**
     * 我的提现申请:askGetMoney
     * $money,$accid
     */
    public function askGetMoney(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $shop = Db::table('up_work_user')->where('user_id',$user_id)
            ->field('user_money,user_status')->find();
        if($shop['user_money'] < $money){
            return json_encode(['status'=>0,'msg'=>'提现金额过大，余额不足！']);
        }
        if($shop['user_status'] == 2){
            return json_encode(['status'=>0,'msg'=>'账户已冻结，不可提现！']);
        }
        if($money < 300){
            return json_encode(['status'=>0,'msg'=>'提现金额不能低于300元']);
        }
        //----------------验证码--------------
        $re = newShopPhonecodeRight($phone,$code,2);
        if($re['status'] != 200){
            return json_encode(['status'=>0,'msg'=>$re['msg']]);
        }
        //-----------验证码end-------------
        $lessmoney = getMoneyLess($money,2);
        Db::startTrans();
        try {
            $data = array();
            $data['r_user_id'] = $user_id;
            $data['r_money'] = $money-$lessmoney;
            $data['r_all_money'] = $money;
            $data['r_less_money'] = $lessmoney;
            $data['r_time'] = date('Y-m-d H:i:s');
            $data['r_water'] = getMillisecond() . rand(100000,999999);
            $data['r_type'] = 2;
            $data['r_where'] = $where;
            if($where == 1){
                $data['r_acc_id'] = $accid;
                $acc = Db::table('up_work_account')->where('acc_id',$accid)->find();
                $data['r_acc_bank'] = $acc['acc_bank'];
                $data['r_acc_username'] = $acc['acc_username'];
                $data['r_acc_num'] = $acc['acc_number'];
            }elseif ($where == 2){
                $data['r_acc_bank'] = '支付宝';
                $data['r_acc_username'] = $name;
                $data['r_acc_num'] = $phone;
            }else{
                $data['r_acc_bank'] = '微信';
                Db::rollback();
                return json_encode(['status'=>0,'msg'=>'暂未开放，敬请期待']);
            }
            Db::table('up_work_jilu')->insert($data);
            Db::table('up_work_user')->where('user_id',$user_id)->setDec('user_money',$money);
            Db::commit();
            Db::table('up_work_phonecode')->where('phone',$phone)->update(['status'=>1]);
            //入消息和推送
            allInNotice('提现申请','您刚刚发起了'.$money.'元的提现，预计会在两小时内到账（节假日除外）',$user_id);
            sendJpush("$user_id", '您刚刚发起了'.$money.'元的提现，预计会在两小时内到账（节假日除外）', array('a' => 'b'), 2, 2, 3);
            return json_encode(['status'=>200,'msg'=>'申请已提交']);
        } catch (\Exception $e) {
            Db::rollback();
            return json_encode(['status'=>0,'msg'=>'操作失败']);
        }
    }

    /**
     * 我的提现记录:myJilu
     * page
     */
    public function myJilu(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $data = Db::table('up_work_jilu')
            ->where('r_user_id',$user_id)
//            ->where('r_status',2)
            ->order('r_time DESC')
            ->page($page,10)
            ->select();
        return json_encode(['status'=>200,'msg'=>$data]);
    }
    /**
     * 我的提现详情：myJiluInfo
     * r_id
     */
    public function myJiluInfo(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $data = Db::table('up_work_jilu')
            ->where('r_id',$r_id)
            ->find();
        return json_encode(['status'=>200,'msg'=>$data]);
    }
    /**
     * 奖励机制：jiangli
     *
     */
    public function jiangli(){
        $data = Db::table('up_about')->where('a_id',2)->find();
        return json_encode(['status'=>200,'msg'=>$data]);
    }
    /**
     * 我的弹性图表：myEcharts
     *
     */
    public function myEcharts(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        if($before > $after){
            return json_encode(['status'=>200,'msg'=>'开始时间不能大于结束时间']);
        }
        $data = Db::table('up_work_order')
            ->where('o_user_id',$user_id)
            ->where('o_status',2)
            ->where('o_agree',2)
            ->where('o_level',1)
            ->where('o_date','between time',[$before,$after])
            ->order('o_datetime')
            ->field('o_date,o_money,o_datetime')
            ->select();
        return json_encode(['status'=>200,'msg'=>$data]);
    }
    /**
     * 业绩及一级团队成员业绩详情：bigData！！！！
     */
    public function bigData(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        //1、用户相关
        $user = Db::table('up_work_user')
            ->where('user_id',$user_id)
            ->field('user_id,user_mynum,user_money')
            ->find();
        //2、今日业绩
        $today = date('Y-m-d');
        $where = array();
        $where['o_user_id'] = $user_id;
        $where['o_status'] = 2;
        $where['o_agree'] = 2;
        $todayData = Db::table('up_work_order')
            ->where($where)
            ->where('o_date',$today)
            ->field('SUM(o_money) as today_money,count(*) as new_shops')
            ->find();//今日开发商户数和今日提成

        $todayData['new_user_one'] = Db::table('up_work_user')
            ->where('user_leadnum',$user['user_mynum'])
            ->where('user_date',$today)
            ->count();//今日新增一级团队数
        $todayData['money_one'] = Db::table('up_work_order')
            ->where($where)
            ->where('o_date',$today)
            ->where('o_level',2)
            ->field('SUM(o_money) as today_two_money')
            ->find()['today_two_money'];//一级提成

        $onenums = Db::table('up_work_user')
            ->where('user_leadnum',$user['user_mynum'])
            ->where('user_date',$today)
            ->column('user_mynum');
        $todayData['new_user_two'] = 0;
        if($onenums){
            $todayData['new_user_two'] = Db::table('up_work_user')
                ->where('user_leadnum','in',$onenums)
                ->where('user_leadnum','>',0)
                ->where('user_date',$today)
                ->count();//今日新增二级团队数
        }
        $todayData['money_two'] = Db::table('up_work_order')
            ->where($where)
            ->where('o_date', $today)
            ->where('o_level', 3)
            ->field('SUM(o_money) as today_thr_money')
            ->find()['today_thr_money'];//二级提成

        //3、总业绩
        $allData = Db::table('up_work_order')
            ->where($where)
            ->field('SUM(o_money) as today_money,count(*) as new_shops')
            ->find();//开发商户数和提成
        $allData['new_user_one'] = Db::table('up_work_user')
            ->where('user_leadnum',$user['user_mynum'])
            ->count();//一级团队数
        $allData['money_one'] = Db::table('up_work_order')
            ->where($where)
            ->where('o_level',2)
            ->field('SUM(o_money) as today_two_money')
            ->find()['today_two_money'];//一级提成
        $allonenums = Db::table('up_work_user')
            ->where('user_leadnum',$user['user_mynum'])
            ->column('user_mynum');
        $allData['new_user_two'] = 0;
        if($allonenums){
            $allData['new_user_two'] = Db::table('up_work_user')
                ->where('user_leadnum','in',$allonenums)
                ->where('user_leadnum','>',0)
                ->count();//二级团队数
        }
        $allData['money_two'] = Db::table('up_work_order')
            ->where($where)
            ->where('o_level', 3)
            ->field('SUM(o_money) as today_thr_money')
            ->find()['today_thr_money'];//二级提成
        $yeji = $this->yeji($user_id,$page);
        return json_encode(['status'=>200,'user'=>$user,'todayData'=>$todayData,'allData'=>$allData,'yeji'=>$yeji]);
    }
    public function yeji($user_id,$page){
        $user = Db::table('up_work_user')
            ->where('user_id',$user_id)
            ->field('user_id,user_mynum,user_money')
            ->find();
        $data = Db::table('up_work_user')
            ->where('user_leadnum',$user['user_mynum'])
            ->field('user_id,user_mynum,user_date')
            ->page($page,15)
            ->select();//一级团队数
        if($data){
            $today = date('Y-m-d');
            foreach($data as $k=>$v){
                $uid = $v['user_id'];
                $u = Db::table('up_work_user')
                    ->where('user_id',$uid)
                    ->field('user_id,user_mynum,user_money')
                    ->find();
                $where = array();
                $where['o_user_id'] = $uid;
                $where['o_status'] = 2;
                $where['o_agree'] = 2;
                $data[$k]['allShop'] = Db::table('up_work_order')
                    ->where($where)
                    ->count();//此人所有商户
                $data[$k]['todayShop'] = Db::table('up_work_order')
                    ->where($where)
                    ->where('o_date', $today)
                    ->count();//此人今日所有商户
                $one = Db::table('up_work_user')
                    ->where('user_leadnum',$u['user_mynum'])
                    ->where('user_leadnum','>',0)
                    ->count();//一级团队数
                $allonenums = Db::table('up_work_user')
                    ->where('user_leadnum',$u['user_mynum'])
                    ->column('user_mynum');
                $two = Db::table('up_work_user')
                    ->where('user_leadnum','in',$allonenums)
                    ->where('user_leadnum','>',0)
                    ->count();//二级团队数
                $data[$k]['allTeam'] = $one+$two;//此人所有团队数
                $one2 = Db::table('up_work_user')
                    ->where('user_leadnum',$u['user_mynum'])
                    ->where('user_date',$today)
                    ->count();//一级团队数
                $allonenums2 = Db::table('up_work_user')
                    ->where('user_leadnum',$u['user_mynum'])
                    ->where('user_date',$today)
                    ->column('user_mynum');
                $two2 = Db::table('up_work_user')
                    ->where('user_leadnum','in',$allonenums2)
                    ->where('user_leadnum','>',0)
                    ->where('user_date',$today)
                    ->count();//二级团队数
                $data[$k]['todayTeam'] = $one2+$two2;//此人今日团队数
            }
        }
        return $data;
    }
}